package com.xianyun.site.iblog.web.blog;

import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.xianyun.site.iblog.api.blog.ArticleServiceI;
import com.xianyun.site.iblog.co.blog.ArticleCO;
import com.xianyun.site.iblog.dto.blog.*;
import com.xianyun.site.iblog.dto.blog.data.ArticleDTO;
import com.xianyun.site.iblog.dto.blog.data.ColumnDTO;
import com.xianyun.site.iblog.dto.blog.data.TagsDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 博客Web控制器
 * @author mac_zyj
 */
@RequestMapping("/blog")
@RestController
public class WebBlogController{

    @Autowired
    private ArticleServiceI articleService;

    
    @ResponseBody
    @PostMapping("/add")
    public Response addBlog(@Valid @RequestBody ArticleAddCmd cmd,
                             HttpServletRequest request){
        cmd.setToken(request.getHeader("Token"));
        cmd.setSessionId(request.getSession().getId());
        return articleService.saveBlogArticle(cmd);
    }

    
    @ResponseBody
    @PostMapping("/update")
    public Response updateBlog(@Valid @RequestBody ArticleAddCmd cmd,
                                HttpServletRequest request){
        cmd.setToken(request.getHeader("Token"));
        cmd.setSessionId(request.getSession().getId());
        Assert.notNull(cmd.getId(),"文章Id不能为空");
        return articleService.updateBlogArticle(cmd);
    }

    
    @ResponseBody
    @PostMapping("/list")
    public PageResponse<ArticleDTO> listBlogBySort(@RequestBody ArticleListSortQry qry,
                                                    HttpServletRequest request){
        qry.setToken(request.getHeader("Token"));
        qry.setSessionId(request.getSession().getId());
        return articleService.selectArticleListBySort(qry);
    }

    
    @ResponseBody
    @GetMapping("/view/listColumns")
    public MultiResponse<ColumnDTO> selectColumnBySort(@RequestBody ColumnSearchQry qry,
                                                       HttpServletRequest request){
        String token= request.getHeader("Token");
        qry.setToken(token);
        qry.setSessionId(request.getSession().getId());
        return articleService.selectUserColumnList(qry);
    }

    @GetMapping("/view/listTags")
    public MultiResponse<TagsDTO> selectTagsBySort(@RequestBody TagSearchQry tagSearchQry,
                                                   HttpServletRequest request){
        String token= request.getHeader("Token");
        tagSearchQry.setToken(token);
        tagSearchQry.setSessionId(request.getSession().getId());
        return articleService.selectUserTagList(tagSearchQry);
    }

    
    @ResponseBody
    @GetMapping("{pathUrl}/article/details/{uniqueId}")
    public SingleResponse<ArticleCO> viewDetailsBlog(@PathVariable("pathUrl") String uniqueUserPath,
                                                      @PathVariable("uniqueId") String uniqueId,
                                                      HttpServletRequest request){
        String token= request.getHeader("Token");
        ArticleViewByUrlQry qry = ArticleViewByUrlQry.builder().
                token(token).userUniquePath(uniqueUserPath).uniqueId(uniqueId).build();
        qry.setSessionId(request.getSession().getId());
        return articleService.getArticleViewByUrl(qry);
    }

    
    @ResponseBody
    @GetMapping("/delete/{blogId}")
    public Response deleteBlogs(@PathVariable("blogId") Integer blogId,
                                 HttpServletRequest request){
        String token= request.getHeader("Token");
        return articleService.deleteBlogArticle(blogId,token);
    }

}
